<template>
	<view>
		<view class="search" @click="searchPicker()" v-show="this.showStation1">
			<u-search v-model="keywords" @custom="search" @search="search" placeholder="请选择厂站"></u-search>
			<u-select v-model="show" :default-value="defaultValue" :list="station" @confirm="confirm"></u-select>
		</view>

		<view class="search" v-show="this.showStation2" style="padding: 24rpx 48rpx;">
			<u-icon name="map" size="36"></u-icon>
			<span>{{keywords}}</span>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				keywords: '程桥街道污水厂',
				defaultValue: [0], //默认选项
				show: false,
				showStation1: false,
				showStation2: false,
				station: [{
						value: '1',
						label: '江'
					},
					// {
					// 	value: '2',
					// 	label: '湖'
					// }
				],
				keyId: "169267375516137"
			};
		},
		created() {
			this.search(this.keywords)
		},
		methods: {
			searchPicker() {
				this.show = true
			},
			confirm(e) {
				e.map((val, index) => {
					this.keywords = val.label;
					this.keyId = val.value;
				})
			},
			search(value) {
				if (value) {
					// this.$u.toast('搜索内容为：' + value)
				}
				this.getStationEq(this.keyId)
			},
			getStationEq(keyId) {
				this.$u.api.search({
					"algorithmProcessId": "getStationEq",
					"param": {
						"data": {
							user_uuid: uni.getStorageSync('uuid')
						}
					}
				}).then(res => {
					if (res && res.data.data.result.length > 1) {
						const station = res.data.data.result
						this.station = station.map(item => {
							return {
								value: item.id,
								label: item.structure_name
							};
						});
						this.showStation1 = true
						this.$emit('search-data', keyId); // 触发自定义事件并传递数据
					} else if (res) {
						const station = res.data.data.result[0]
						this.station[0].value = station.id
						this.station[0].label = station.structure_name
						this.keywords = this.station[0].label
						this.showStation2 = true
						this.$emit('search-data', this.station[0].value); // 触发自定义事件并传递数据
					} else {
						this.$u.toast(res.message);
					}

				}).catch(error => {
					this.$u.toast('查询异常');
					console.error(error);
				});
			}
		}
	};
</script>

<style scoped>
	page {
		background-color: #f5f5f5;
	}

	.wrap .search {
		background: #ffffff;
	}
</style>